/* Departemental level estimates */
/* LOCATION SHIFT */
clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

/***************/
/* Estimations */
/***************/
capture program drop regsamp
program define regsamp
version 11
syntax, occp(name) outc(name)

use dep_final.dta, clear

gen reg=substr(Xze,1,2)
/*gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94" */

xi , pre(REG_) i.reg

gen occ="bcq" if bcq==1
replace occ="cad" if cad==1

/* adjust for ile de France mobility */
gen idf=(ze=="75" | ze=="77" | ze=="78" | ze=="91" |ze=="92" |ze=="93" |ze=="94" |ze=="95" )
gen aidf=(aze=="75" | aze=="77" | aze=="78" | aze=="91" | aze=="92" | aze=="93" | aze=="94" | aze=="95" )


/* create some outcome variables */
gen ldp=ln(dp/l.dp)
gen dftfy=(dp==360)-(L.dp==360)
gen oshift=(`occp'==0 & L.`occp'==1)
replace oshift=( (cad==0 & L.cad==1) | (tecemp2==0 & L.tecemp2==1) | (bcq==0 & L.bcq==1) | (bcnq==0 & L.bcnq==1)) if "`occp'"=="all"

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
/* residual outcome */
quietly: reg `outc' F_*  if btime==2
predict r`outc' if btime==2, residuals

/* rank in the wage distribution */
drop F_*
egen qinit = fastxtile(rlsnd) if ftreat, by(ze an btime) nq(100)
replace qinit=qinit/100

/* interaction term and instruments */
gen lqinit=L.qinit
gen inter= dimm*L.qinit
gen linter=ldimm*L.qinit
gen inter75= dimm75 * L.qinit
gen linter75=ldimm75*L.qinit
gen interm2= dimtm2 * L.qinit
gen linterm2=ldimtm2 *L.qinit

keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse r`outc' dimm dimm75 Y_* REG_* , by(ze an)

/* OLS */
ivreg2 r`outc' dimm Y_*  REG_* , partial(Y_* REG_* ) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 r`outc' (dimm  = dimm75 ) Y_* REG_* , partial(Y_* REG_* ) cl(ze)
estimates store `occp'iv

use temp1.dta, clear
ivreg2 r`outc' (dimm inter = dimm75 inter75) lqinit  Y_*  REG_* [aweight = iw2], partial(Y_* REG_* ) cl(ze)
estimates store `occp'ivs

test dimm + inter = 0
end

capture program drop mobtyp
program define mobtyp
version 11
syntax, outc(name) file(name) 

/* OUTFLOWS */
regsamp, occp(all) outc(`outc')
regsamp, occp(cad) outc(`outc')
regsamp, occp(tecemp2) outc(`outc')
regsamp, occp(bcq) outc(`outc')
regsamp, occp(bcnq) outc(`outc')

capture rm "C:\Users\Public\Documents\Prog20\TablesRev2020\\`file'.rtf"
/* OLS */
esttab allols  cadols tecemp2ols bcqols bcnqols /* bcols tradols ntradols constbcols */ ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\\`file'.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/* IV */
esttab alliv  cadiv tecemp2iv bcqiv bcnqiv /* bciv tradiv ntradiv constbciv */ ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\\`file'.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/* IVSELECT */
esttab allivs  cadivs tecemp2ivs bcqivs bcnqivs /* bcivs tradivs ntradivs constbcivs */ ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\\`file'.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

end

mobtyp , outc(lshift) file(dep_outflows)

/* Departemental level estimates */
/* LOCATION SHIFT WITHOUT IDF */
clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

/***************/
/* Estimations */
/***************/
capture program drop regsamp
program define regsamp
version 11
syntax, occp(name) outc(name)

use dep_final.dta, clear

gen reg=substr(Xze,1,2)
/*gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94" */

xi , pre(REG_) i.reg

gen occ="bcq" if bcq==1
replace occ="cad" if cad==1

/* adjust for ile de France mobility */
gen idf=(ze=="75" | ze=="77" | ze=="78" | ze=="91" |ze=="92" |ze=="93" |ze=="94" |ze=="95" )
gen aidf=(aze=="75" | aze=="77" | aze=="78" | aze=="91" | aze=="92" | aze=="93" | aze=="94" | aze=="95" )

/*replace lshift=0 if idf==1 & aidf==1 & lshift==1*/
drop if idf==1

/* create some outcome variables */
gen ldp=ln(dp/l.dp)
gen dftfy=(dp==360)-(L.dp==360)
gen oshift=(`occp'==0 & L.`occp'==1)
replace oshift=( (cad==0 & L.cad==1) | (tecemp2==0 & L.tecemp2==1) | (bcq==0 & L.bcq==1) | (bcnq==0 & L.bcnq==1)) if "`occp'"=="all"

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
/* residual outcome */
quietly: reg `outc' F_*  if btime==2
predict r`outc' if btime==2, residuals

/* rank in the wage distribution */
drop F_*
egen qinit = fastxtile(rlsnd) if ftreat, by(ze an btime) nq(100)
replace qinit=qinit/100

/* interaction term and instruments */
gen lqinit=L.qinit
gen inter= dimm*L.qinit
gen linter=ldimm*L.qinit
gen inter75= dimm75 * L.qinit
gen linter75=ldimm75*L.qinit
gen interm2= dimtm2 * L.qinit
gen linterm2=ldimtm2 *L.qinit

keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse r`outc' dimm dimm75 Y_* REG_* , by(ze an)

/* OLS */
ivreg2 r`outc' dimm Y_*  REG_* , partial(Y_* REG_* ) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 r`outc' (dimm  = dimm75 ) Y_* REG_* , partial(Y_* REG_* ) cl(ze)
estimates store `occp'iv

use temp1.dta, clear
ivreg2 r`outc' (dimm inter = dimm75 inter75) lqinit  Y_*  REG_* [aweight = iw2], partial(Y_* REG_* ) cl(ze)
estimates store `occp'ivs

test dimm + inter = 0
end

capture program drop mobtyp
program define mobtyp
version 11
syntax, outc(name) file(name) 

/* OUTFLOWS */
regsamp, occp(all) outc(`outc')
regsamp, occp(cad) outc(`outc')
regsamp, occp(tecemp2) outc(`outc')
regsamp, occp(bcq) outc(`outc')
regsamp, occp(bcnq) outc(`outc')

capture rm "C:\Users\Public\Documents\Prog20\TablesRev2020\\`file'.rtf"
/* OLS */
esttab allols  cadols tecemp2ols bcqols bcnqols /* bcols tradols ntradols constbcols */ ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\\`file'.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/* IV */
esttab alliv  cadiv tecemp2iv bcqiv bcnqiv /* bciv tradiv ntradiv constbciv */ ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\\`file'.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/* IVSELECT */
esttab allivs  cadivs tecemp2ivs bcqivs bcnqivs /* bcivs tradivs ntradivs constbcivs */ ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\\`file'.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

end

mobtyp , outc(lshift) file(dep_outflows_NOIDF)

/*********************/
/* Departement WAGES */
/*********************/
capture program drop wages
program define wages
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use dep_final.dta, clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drlsnd dimm dimm75 Y_*, by(ze an)

/* OLS */
ivreg2 drlsnd dimm Y_*  , partial(Y_* ) cl(ze)
estimates store bs`occp'ols

/* IV */
ivreg2 drlsnd (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store bs`occp'iv

end

wages, occp(all) 
wages, occp(cad) 
wages, occp(tecemp2) 
wages, occp(bc) 
wages, occp(bcnq) 

capture rm "C:\Users\Public\Documents\Prog20\TablesRev2020\dep_wages.rtf"
/* IV Balanced */
esttab bsalliv  bscadiv bstecemp2iv bsbciv bsbcnqiv ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\dep_wages.rtf" /// 
, append title("bs")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
 
 /***************************/
/* Departement WAGES ANNUAL */
/****************************/
capture program drop wages
program define wages
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use dep_final.dta, clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drlsnd dimm dimm75 Y_*, by(ze an)

/* OLS */
ivreg2 drlsnd dimm Y_*  , partial(Y_* ) cl(ze)
estimates store bs`occp'ols

/* IV */
ivreg2 drlsnd (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store bs`occp'iv

end

wages, occp(all) 
wages, occp(cad) 
wages, occp(tecemp2) 
wages, occp(bc) 
wages, occp(bcnq) 

capture rm "C:\Users\Public\Documents\Prog20\TablesRev2020\dep_wages_annual.rtf"
/* IV Balanced */
esttab bsalliv  bscadiv bstecemp2iv bsbciv bsbcnqiv ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\dep_wages_annual.rtf" /// 
, append title("bs")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
 
/****************************************************/
/* Cross-sectional OLS and IV wages for departement */
/****************************************************/
/* share immigrants in CZE */
/* construct regression table at the DEPARTEMENTAL level */
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
/* share immigrants in CZE */
use "ImmPanelRevis19\Donnees\pit\dep_instru2017.dta", clear
keep an ze zeimmig mlsnd zepop
rename ze aze
rename mlsnd amlsnd 
rename zepop azepop
save tempAZE.dta, replace

use "ImmPanelRevis19\donnees\indivm.dta", clear
/***************************************************/
/* Construct the table final.dta for the estimates */
/***************************************************/
/**************************************/
/* Définition des zones géographiques */
/**************************************/
/**************************************/
/* 1) => Merge avec les ZE 			  */
/**************************************/
/* elimine la corse */
drop if dep=="2A" | dep=="2B"

gen cgeo=dep+comt
sort cgeo
/* Paris */
replace cgeo="75056" if dep=="75"
/* Lyon */
replace cgeo="69123" if cgeo=="69381" | cgeo=="69382" | cgeo=="69383" | cgeo=="69384" | cgeo=="69385" | cgeo=="69386" | cgeo=="69387" | cgeo=="69388" | cgeo=="69389" 
/* Marseille */
replace cgeo="13055" if cgeo=="13201" | cgeo=="13202" | cgeo=="13203" | cgeo=="13204" | cgeo=="13205" | cgeo=="13206" | cgeo=="13207" | cgeo=="13208" | cgeo=="13209" | cgeo=="13210" | cgeo=="13211" | cgeo=="13212" | cgeo=="13213" | cgeo=="13214" | cgeo=="13215" | cgeo=="13216"

/* définit les ZE */
joinby cgeo using "ImmPanelRevis19\donnees\cgeo_ze.dta", unm(m)
tab _merge
drop if _merge==1
drop _merge
drop zen

/* foreign born workers */
gen fborn=(depnai=="99")
gen ifborn="N" if fborn==0
replace ifborn="FB" if fborn==1
drop if fborn

/*********************/
/* add task contents */
/*********************/
joinby cs2h using "ImmPanelRevis19\Donnees\Onet\CS2_OnetNorm.dta", unm(m)
tab _merge
drop _merge

save temp.dta, replace

/***************************/
/* Add contemporary groups */
/***************************/
use "ImmPanelRevis19\donnees\ident_groupmw.dta", clear
rename gref an
save gref.dta, replace

use temp.dta, clear
joinby nninouv an using gref.dta, unm(m)
tab _merge
drop _merge

egen time=group(an)
xtset indiv time
compress

save temp.dta, replace

replace ze=dep
/******************************************/
/* Merge with immigration shocks location */
/******************************************/
joinby ze an using "ImmPanelRevis19\Donnees\pit\dep_instru2017.dta", unm(m)
tab _merge
drop _merge


/*********************/
/* change in outcome */
/*********************/
egen indiv2=group(nninouv)
xtset indiv2 time
sort indiv2 time

/**************/
/* occupation */
/**************/
/* Changes in task intensity */
gen dzbasman=D.zbasman 
gen dzbascom=D.zbascom 

/* fixed effects */
xi, pre(Y_) i.an*lsconst i.an*lstrad i.an*lsntrad i.an*lzepop

/* location shifter */
egen ize=group(ze)
gen lshift=(ize!=L.ize)
/* eliminate small CZ */
drop if ze=="9306" | ze=="9109" | ze=="9110" | ze=="9116" | ze=="8307" | ze=="7206" | ze=="5214" 
drop if ze=="4101" | ze=="4111" | ze=="8306"

keep if age>=25 & age<=59
/* fixed effects age x an */
xi i.age*i.an , pre(F_)

/* first period of obs treated */
gen ptreat=(age>=25 & age<=50)
/* second period of obs treated */
gen dtreat=(age>=31 & age<=56) if an==1982
replace dtreat=(age>=34 & age<=59) if an==1991
replace dtreat=(age>=33 & age<=58) if an==1999
replace dtreat=(age>=33 & age<=58) if an==2007

gen tecemp2=(tec==1 | emp2==1)

save "ImmPanelRevis19\Donnees\select\dep_unbalanced.dta", replace

/********************/
/* Wages unbalanced */
/********************/
capture program drop wagescs
program define wagescs
version 11
syntax, occp(name)

use "ImmPanelRevis19\Donnees\select\dep_unbalanced.dta", clear
keep if `occp'==1

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* if ptreat | dtreat
predict rlsnd, residuals

save temp1.dta, replace
collapse (mean) prlsnd=rlsnd if ptreat, by(ze an)
save p1.dta, replace
use temp1.dta, clear
collapse (mean) drlsnd=rlsnd if dtreat, by(ze an)
save p2.dta, replace
use temp1.dta, clear
collapse dimm dimm75 Y_*, by(ze an)
joinby ze an using p1.dta, unm(m)
drop _merge
joinby ze an using p2.dta, unm(m)
drop _merge

egen ize=group(ze)
egen time=group(an)
xtset ize time
gen rlsnd=drlsnd-L.prlsnd
/* OLS */
ivreg2 rlsnd dimm Y_*  , partial(Y_* ) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 rlsnd (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store `occp'iv
end

wagescs, occp(all) 
wagescs, occp(cad) 
wagescs, occp(tecemp2) 
wagescs, occp(bcq) 
wagescs, occp(bcnq) 

capture rm "C:\Users\Public\Documents\Prog20\TablesRev2020\dep_wagescs.rtf"
/* OLS */
esttab allols  cadols tecemp2ols bcqols bcnqols ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\dep_wagescs.rtf" /// 
, append title("OLS_CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N rkf) star(* 0.10 ** 0.05 *** 0.01)

/* IV */
esttab alliv  cadiv tecemp2iv bcqiv bcnqiv ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\dep_wagescs.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N rkf) star(* 0.10 ** 0.05 *** 0.01)
 
 
/* inflows into the DEPARTEMENT */
/*************************************/
/* Inflows into the occupation group */
/*************************************/
capture program drop inflows
program define inflows
version 11
syntax, occp(name)

use "ImmPanelRevis19\Donnees\select\dep_unbalanced.dta", clear
replace cad2=(cs2h=="33" | cs2h=="34" | cs2h=="37" | cs2h=="38")

gen orig=(ptreat==1 & `occp'==1 )
/* gen inflow=(dtreat==1 & bc==1 & (L.bc!=1 | L.ize!=ize) & !missing(L.ize)) */
gen inflow=(dtreat==1 & `occp'==1 & L.ize!=ize )

gen sond=12
replace sond=24 if an==2007

save temp1.dta, replace
collapse (sum) norig=orig ninflow=inflow [fweight = sond], by(ze an)
save p1.dta, replace
use temp1.dta, clear
collapse dimm dimm75 Y_* , by(ze an)
joinby ze an using p1.dta, unm(m)
drop _merge

egen ize=group(ze)
egen time=group(an)
xtset ize time
gen inflr=(D.ninflow)/norig

drop if missing(dimm75)
/* OLS */
ivreg2 inflr dimm Y_*  , partial(Y_* ) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 inflr (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store `occp'iv

end

inflows, occp(all) 
inflows, occp(cad2) 
inflows, occp(tecemp2) 
inflows, occp(bcq) 
inflows, occp(bcnq) 


capture rm "C:\Users\Public\Documents\Prog20\TablesRev2020\DEP_inflows_unb.rtf"
/* OLS */
esttab allols  cad2ols tecemp2ols bcqols bcnqols ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\DEP_inflows_unb.rtf" /// 
, append title("OLS_CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/* IV */
esttab alliv  cad2iv tecemp2iv bcqiv bcnqiv ///
using "C:\Users\Public\Documents\Prog20\TablesRev2020\DEP_inflows_unb.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
